<?php

namespace App\Http\Controllers\scheduleController;

use Illuminate\Http\Request;
use App\Models\ScheduleTask;
use App\Services\ScheduleServiceInterFace;
use Illuminate\Support\Facades\Validator;
use App\Pojo\Response;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;

class ScheduleTaskController extends Controller
{
    protected $scheduleService;

    public function __construct(ScheduleServiceInterFace $scheduleService)
    {
        $this->scheduleService = $scheduleService;
    }

    /**
     * 显示所有任务
     */
    public function index(Request $request)
    {
        $credentials = $request->validate([
            'page' => 'required|integer',
            'per_page' => 'required|integer',
        ]);
        $data = $this->scheduleService->getTaskList($credentials);
        return Response::success($data, '任务查询成功', 200);
    }

    /**
     * 创建新任务
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'data.name' => 'required|string',
            'data.cron' => 'required|string',
            'data.time_type' => 'required|string',
            'data.delay' => 'nullable|string',
            'data.jitter_time' => 'nullable|string',
        ]);
   
        if ($validator->fails()) {
            return Response::error($validator->errors()->first(), 422);
        }
        $data = $validator->validated();
        $credentials = $data['data'];
        $this->scheduleService->saveTask($credentials);
        return Response::success(null, '任务保存成功', 200);
    }

    /**
     * 显示特定任务
     */
    public function show($id)
    {
        $task = ScheduleTask::findOrFail($id);
        return response()->json([
            'status' => 'success',
            'data' => $task
        ]);
    }

    /**
     * 更新任务
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'string|max:255|unique:schedule_tasks,name,' . $id,
            'description' => 'nullable|string',
            'command' => 'nullable|string',
            'cron_expression' => 'string',
            'is_active' => 'boolean'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'errors' => $validator->errors()
            ], 422);
        }

        $data = $request->all();
        $data['id'] = $id;

        $task = $this->scheduleService->saveTask($data);

        return response()->json([
            'status' => 'success',
            'message' => '任务更新成功',
            'data' => $task
        ]);
    }

    /**
     * 删除任务
     */
    public function destroy($id)
    {   
        $this->scheduleService->deleteTask($id);
        return Response::success(null,'任务删除成功', 200);
        
    }

    /**
     * 启用/禁用任务
     */
    public function toggleStatus(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'data.action' => 'required|boolean'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'errors' => $validator->errors()
            ], 422);
        }

        $data = $validator->validated();
        $toggleData = json_encode($data['data']['action']);
        
        $task = $this->scheduleService->toggleTaskStatus($id, $toggleData);
        
        $status = $task['is_active'] ? '启用': '暂停';
        return response::success($task,  "任务已{$status}");
    }
} 